from typing import List
from fastapi import APIRouter, Depends, HTTPException, Request
from modules.vo.dept_vo import DeptModel, DeptQueryModel
from modules.service.dept_service import DeptService
from utils.response_util import ResponseUtil
from logs.logger.log_util import logger
from modules.schemas.dept_view import SysDeptVo
from datetime import datetime

deptController = APIRouter(prefix="/system/dept")


@deptController.get("/list")
async def get_system_dept_list(request: Request,
                               dept_query: DeptQueryModel = Depends(DeptQueryModel.as_query)):
    """

    """
    depts_query_result = await DeptService.get_dept_list_services(dept_query)
    if depts_query_result:
        logger.info(f"get_system_dept_list, depts_query_result 查询成功 :{depts_query_result}")
    result = [SysDeptVo.from_orm(row) for row in depts_query_result]
    # print("   ================  ", result, type(result))
    return ResponseUtil.success(data=result)


@deptController.post("/add_dept", response_model=DeptModel)
async def add_system_dept(request: Request, dept_model: DeptModel):
    """

    """
    dept_model.create_time = datetime.now()
    dept_model.update_time = datetime.now()
    logger.info(f"add_system_dept, dept_model 新增部门信息 :{dept_model}")
    dept_add_result = await DeptService.add_dept_services(dept_model)
    logger.info(f"add_system_dept, dept_add_result 添加成功 :{dept_add_result}")
    return ResponseUtil.success(data=dept_add_result)
